package com.biscuit.page.basicservice.design.websocket.register.strategy;

import com.biscuit.page.basicservice.design.websocket.register.AbstractWebSocketRegister;
import com.biscuit.page.basicservice.design.websocket.register.ChatRoomWebSocketRegisterContext;
import com.biscuit.page.basicservice.websocket.chat_room.ChatRoomWebSocketContext;
import com.biscuit.page.basicservice.websocket.chat_room.ChatRoomWebSocketHandler;

/**
 * @author biscuit
 * @createTime 2024/6/22 14:34
 */
public class ChatRoomWebSocketRegisterHandler implements WebSocketRegisterHandlerStrategy {

    @Override
    public void register(AbstractWebSocketRegister context) {
        // 将类型转换为实际入参类型
        ChatRoomWebSocketRegisterContext chatRoomContext = (ChatRoomWebSocketRegisterContext) context;

        // 构建聊天室 websocket 上下文对象
        ChatRoomWebSocketContext chatRoomWebSocketContext = ChatRoomWebSocketContext.builder()
                .chatRoomId(chatRoomContext.getChatRoomId())
                .userUid(chatRoomContext.getUserUid())
                .session(chatRoomContext.getSession())
                .build();

        // 保存用户在线上下文对象信息到指定聊天室集合
        ChatRoomWebSocketHandler.putByChatRoom(chatRoomContext.getChatRoomId(), chatRoomWebSocketContext);
    }

}
